<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane :label="item.name" :name="item.id" v-for="item in tabList" :key="item.id"></el-tab-pane>
      </el-tabs>
      <div class="tab-body">
        <!--        <div class="tab-box">-->
        <!--          &lt;!&ndash;          // 0待审核1通过2驳回3借出4归还&ndash;&gt;-->
        <!--          <el-radio-group v-model="radioStatus" size="small" @change="changeFn()">-->
        <!--            <el-radio-button label="all">全部({{ number ? number.all : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="0">申请中({{ number ? number.apply : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="3">待归还({{ number ? number.lend : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="4">已归还({{ number ? number.end : 0 }})</el-radio-button>-->
        <!--          </el-radio-group>-->
        <!--        </div>-->
        <div class="tab-box-list mb-10">
          <el-select v-model="editForm.status" placeholder="选择状态" size="small" style="width: 140px" class="mr-10"
                     v-if="Number(activeName)==1">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in statusList"
                       :key="index"></el-option>
          </el-select>
          <el-select v-model="readStatus" placeholder="选择状态" size="small" style="width: 140px" class="mr-10" v-else>
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in readList"
                       :key="index"></el-option>
          </el-select>
          <el-button size="small" type="primary" plain @click="search('search')">搜索</el-button>
          <el-button size="small" plain @click="search('resct')">重置</el-button>
          <el-button size="small" type="primary" plain @click="addStudent" v-if="Number(activeName)==1"><i
              class="el-icon-plus"></i>申请使用证照
          </el-button>
        </div>
      </div>

      <!--本体列表，数据 -->
      <div class="table-body">
        <div class="table-body-box">
          <el-table
              ref="multipleTable"
              :data="arryList"
              :stripe="tableStyle.tableStripe"
              :border="tableStyle.tableBorder"
              tooltip-effect="dark"
              header-cell-class-name="table-text-c"
              style="width: 100%">
            <el-table-column
                prop="name"
                label="证照名称"
                width="120"
                :key="1"
                align="center">
              <template slot-scope="scope">
                {{ scope.row.licence.name }}
              </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="当前持有人"
                :key="2"
                :show-overflow-tooltip="true"
                align="center">
              <template slot-scope="scope">
                {{ scope.row.user ? scope.row.user.realname : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                label="申请人"
                align="center"
                :key="3"
                :show-overflow-tooltip="true">
              <template slot-scope="scope">
                {{ scope.row.user ? scope.row.user.realname : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                prop="purpose"
                label="用途"
                :key="4"
                align="center"/>
            <el-table-column
                prop="time"
                :key="5"
                label="预借用日期"
                align="center"/>
            <el-table-column
                prop="return_time"
                label="预归还日期"
                :key="6"
                align="center"/>
            <el-table-column
                prop="overtime"
                label="剩余天数"
                align="center"
                :key="7"
                v-if="radioStatus==3 || radioStatus=='all'"/>
            <el-table-column
                prop="examine_time"
                label="审核时间"
                align="center"
                :key="8"
                width="100">
            </el-table-column>
            <!--            <el-table-column-->
            <!--                prop="remark"-->
            <!--                label="补充说明"-->
            <!--                align="center"/>-->
            <!--            <el-table-column-->
            <!--                prop="mobile_phone"-->
            <!--                label="归还照片"-->
            <!--                align="center"-->
            <!--                :key="9"-->
            <!--                v-if="radioStatus==4 || radioStatus=='all'">-->
            <!--              <template slot-scope="scope">-->
            <!--                &lt;!&ndash;                <viewerModle type="1" :imgSrc="scope.row.return_pic"></viewerModle>&ndash;&gt;-->
            <!--                <ImagePreview :src="scope.row.return_pic" width="40px" height="40px"></ImagePreview>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column
                prop="status"
                label="状态"
                :key="9"
                v-if="Number(activeName)==1"
                align="center">
              <template slot-scope="scope">
                <!--                // 0待审核1通过2驳回3借出4归还-->
                <!--                申请通过 已驳回 待归还 已归还   -->
                <el-tag type="warning" v-if="scope.row.status==0">待审核</el-tag>
                <el-tag v-if="scope.row.status==1">待借出</el-tag>
                <!--                <el-tag type="success" v-if="scope.row.status==1">通过</el-tag>-->
                <el-tag type="danger" v-if="scope.row.status==2">已驳回</el-tag>
                <el-tag v-if="scope.row.status==3">待归还</el-tag>
                <el-tag type="success" v-if="scope.row.status==4">已归还</el-tag>

                <!--                <span v-if="scope.row.status==0">待审核</span>-->
                <!--                <span v-if="scope.row.status==2" class="examinBtn">已驳回 <i>驳回说明：{{ scope.row.content }}</i></span>-->
                <!--                <span v-if="scope.row.status==3">待归还</span>-->
                <!--                <span v-if="scope.row.status==4">已归还</span>-->
              </template>
            </el-table-column>
            <el-table-column
                label="状态"
                v-if="Number(activeName)==2"
                align="center"
                :key="8"
                width="100">
              <template slot-scope="scope">
                {{scope.row.is_read_user?'已读':'未读'}}
              </template>
            </el-table-column>
            <el-table-column
                label="操作"
                :key="11"
                width="150"
                fixed="right">
              <template slot-scope="scope">
                <el-button plain size="small" @click="detailId=scope.row.id,detailUsevisible=true">
                  详情
                </el-button>
                <el-button plain size="small" @click="deletWx(scope.row)" type="danger"
                           v-if="scope.row.status==0 && Number(activeName)==1">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </div>
    <applyLicence :visible.sync="detailVisible" @InitFn="InitFn"></applyLicence>
    <cardDetail :visible.sync="detailUsevisible" :detailId="detailId" @InitFn="InitFn" pageType="my"></cardDetail>
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js'
import {licence_apply_delete} from '@/api/admin/badge'
import applyLicence from "./applyLicence";
import cardDetail from '../components/detail'
import {mapGetters} from "vuex";

export default {
  components: {
    applyLicence,
    cardDetail
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      radioStatus: 'all',
      detailVisible: false,
      detailUsevisible: false,
      detailId: 0,
      licenceId: 0,
      handleSelection: [],
      arryList: [], // 页面数据列表
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      indexOption: '',
      classesListNew: [],
      sectionList: [],
      classesList: [],
      // 搜索组件
      searchForm: {},
      parentsVisible: false,
      parentsOption: '',
      number: null,
      editForm: {status: 99},
      statusList: [
        {
          id: 99,
          name: '全部'
        },
        {
          id: 0,
          name: '申请中'
        },
        {
          id: 3,
          name: '待归还'
        },
        {
          id: 4,
          name: '已归还'
        }
      ],
      activeName: '1',
      tabList: [
        {
          name: '我的证照',
          id: '1',
        },
        {
          name: '我的抄送',
          id: '2'
        }
      ],
      readStatus: 99,
      readList: [
        {
          id: 99,
          name: '全部'
        },
        {
          id: 0,
          name: '未读'
        },
        {
          id: 1,
          name: '已读'
        }
      ],
    }
  },
  watch: {
    activeName() {
      this.search('resct')
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      // 0待审核1通过2驳回3借出4归还
      let option_ = {p: this.page.pageindex}
      if (Number(this.activeName) === 1) {
        option_.user_id = this.userInfo.id
      } else {
        option_.type = 3
        if (this.readStatus !== 99) {
          option_.is_read = this.readStatus
        }
      }
      // is_read 不传全部 0 未读 1 已读
      if (this.editForm.status !== 99) {
        option_.status = this.editForm.status;
      }


      this.$fetch(this.api.licence.apply_list, option_).then((response) => {
        if (response.code === 0) {
          for (let i in response.list.data) {
            if (response.list.data[i].status === 3) {
              let day1 = new Date(response.list.data[i].return_time);
              // let day1 = new Date('2021-11-25')
              let day2 = new Date();
              let overTime = (day2 - day1) / (1000 * 60 * 60 * 24);
              if (overTime > 0) {
                let time_end = parseInt(overTime)
                response.list.data[i].overtime = time_end + '天'
              } else {
                response.list.data[i].overtime = 0 + '天'
              }
            } else {
              response.list.data[i].overtime = '--'
            }
          }
          this.arryList = JSON.parse(JSON.stringify(response.list.data))
          this.page.total = response.list.total;
          this.number = response.number;
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    changeFn() {
      this.InitFn()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    search(option) {
      this.page.pageindex = 1;
      if (option !== 'search') {
        // this.editForm.name = '';
        this.editForm.status = 99;
        this.readStatus = 99
      }
      this.InitFn()
    },
    addStudent(row) {
      this.licenceId = row || '';
      this.detailVisible = true;
    },
    deletWx(row) {
      let n = this;
      this.$confirm('确认删除该申请, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.closeBinding(row)
      }).catch(() => {
      })
    },
    closeBinding(row) {
      licence_apply_delete({id: row.id}).then(res => {
        if (!res.code) {
          this.$message.success(res.msg)
          this.InitFn()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

  },
  mounted() {
  },
  // 缓存机制 end
}
</script>

<style scoped>

.wx-user-box-line img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.wx-user-box-line {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-wx {
  cursor: pointer;
  padding: 5px;
}

.btn-wx:hover {
  color: #1b8de5;
  text-decoration: underline;
}

.delet-wx {
  color: red;
  cursor: pointer;
}

.delet-wx:hover {
  text-decoration: underline;
}

.tab-body {
  display: flex;
  align-items: center;
}

.tab-box {
  flex: 1;
}

.examinBtn {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: red;
}

.examinBtn i {
  font-style: normal;
  color: #1b8de5;
  cursor: pointer;
  font-size: 12px;
}
</style>
